package com.acoffee.controller;

import com.acoffee.pojo.RbacRole;
import com.acoffee.pojo.ResponseEntity;
import com.acoffee.service.RbacRoleService;
import com.acoffee.service.RbacRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author acoffee
 * @create 2021-11-27 15:17
 */

@RestController
public class RbacRoleController {
    @Autowired
    RbacRoleService rbacRoleService;

    @GetMapping("/queryAllRole/{startPage}/{pageSize}")
    public ResponseEntity<PageInfo<RbacRole>> queryAll(@PathVariable int startPage, @PathVariable int pageSize){
        PageHelper.startPage(startPage,pageSize);
        List<RbacRole> rbacRoles = rbacRoleService.queryAll();
        PageInfo<RbacRole> pageInfo = new PageInfo<>(rbacRoles);
        ResponseEntity<PageInfo<RbacRole>> entity = new ResponseEntity<>();
        entity.setCode(200);
        entity.setMsg("查询成功");
        entity.setData(pageInfo);
        return entity;
    }

    @GetMapping("/queryRoleById/{id}")
    public ResponseEntity<RbacRole> queryOne(@PathVariable int id){
        RbacRole Role = rbacRoleService.queryOne(id);
        ResponseEntity<RbacRole> entity = new ResponseEntity<>();
        entity.setCode(200);
        entity.setMsg("查询成功");
        entity.setData(Role);
        return entity;
    }

    @PostMapping("/updateRoleById")
    public ResponseEntity updateOne(RbacRole Role){
        ResponseEntity<Object> entity = new ResponseEntity<>();
        boolean flag = rbacRoleService.updateOne(Role);
        if(flag){
            entity.setCode(200);
            entity.setMsg("修改成功！");
            entity.setData(Role);
        }else{
            entity.setCode(303);
            entity.setMsg("修改失败！");
        }
        return entity;
    }

    @PutMapping("/addRole")
    public ResponseEntity addOne(RbacRole Role){
        boolean flag = rbacRoleService.addOne(Role);
        ResponseEntity<Object> entity = new ResponseEntity<>();
        if(flag){
            entity.setCode(200);
            entity.setMsg("添加成功！");
            entity.setData(Role);
        }else{
            entity.setCode(303);
            entity.setMsg("添加失败！");
        }
        return entity;
    }

    @DeleteMapping("/deleteRole/{id}")
    public ResponseEntity deleteOne(@PathVariable int id){
        ResponseEntity<Object> entity = new ResponseEntity<>();
        boolean flag = rbacRoleService.deleteOne(id);
        if(flag){
            entity.setCode(200);
            entity.setMsg("删除成功！");
        }else{
            entity.setCode(303);
            entity.setMsg("删除失败！");
        }
        return entity;
    }
}
